package czy.demo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/*
 * swagger配置，主要配置了Docket(案卷)，ApiInfo(api 信息)
 * swagger文档JSON信息rest接口/v2/api-docs
 * swagger文档页面swagger-ui
 */
@Configuration
@EnableSwagger2
public class SpringSwaggerConfig {

    @Configuration
    public class Swagger2 {

        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("czy.demo.controller"))
                    .paths(PathSelectors.any())
                    .build()
                    .securityContexts(securityContexts())
                    .securitySchemes(securitySchemes());
        }

        /* API信息 */
        private ApiInfo apiInfo() {

            Contact contact = new Contact("crayon-shin-chan","https://github.com/crayon-shin-chan","1253665625@qq.com");

            return new ApiInfoBuilder()
                    /* API标题 */
                    .title("spring redis session 测试")
                    /* API描述 */
                    .description("简单优雅的restfun风格")
                    /* 作者联系方式？ */
                    .termsOfServiceUrl("https://blog.csdn.net/qq_27868061")
                    /* 版本 */
                    .version("1.0")
                    /* 许可 */
                    .license("Apache License")
                    .contact(contact)
                    .build();
        }

        /* 安全模式，这里指定token通过Authorization头请求头传递 */
        private List<ApiKey> securitySchemes() {
            List<ApiKey> apiKeyList= new ArrayList<>();
            apiKeyList.add(new ApiKey(SpringSessionConfig.SESSION_HEADER, SpringSessionConfig.SESSION_HEADER, "header"));
            return apiKeyList;
        }

        /* 安全上下文 */
        private List<SecurityContext> securityContexts() {
            List<SecurityContext> securityContexts=new ArrayList<>();
            securityContexts.add(
                    SecurityContext.builder()
                            .securityReferences(defaultAuth())
                            .forPaths(PathSelectors.any())
                            .build());
            return securityContexts;
        }

        /* 默认的安全饮用 */
        List<SecurityReference> defaultAuth() {
            AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
            AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
            authorizationScopes[0] = authorizationScope;
            List<SecurityReference> securityReferences= new ArrayList<>();
            securityReferences.add(new SecurityReference(SpringSessionConfig.SESSION_HEADER, authorizationScopes));
            return securityReferences;
        }
    }
}
